package com.zmt.mrms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserDoctor;
import com.zmt.mrms.service.DoctorService;

import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/doctor")
public class DoctorController {

	@Resource
	private DoctorService doctorService;

	private HttpServletRequest request;
	private HttpServletResponse response;

	protected DoctorController(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	protected void setCookie(String id, String role) {
		Cookie cookie1 = new Cookie("id", id);
		Cookie cookie2 = new Cookie("role", role);
		cookie1.setHttpOnly(true);
		cookie2.setHttpOnly(true);
		cookie1.setMaxAge(-1);
		cookie2.setMaxAge(-1);
		cookie1.setPath("/");
		cookie2.setPath("/");
		this.response.addCookie(cookie1);
		this.response.addCookie(cookie2);
	}

	@GetMapping("")
	public String homePage() {
		return "redirect:/doctor/personal";
	}

	@GetMapping("/medicine/getMedinice")
	public String getMedinice(Model model) {
		model.addAttribute("map", doctorService.getPrescriptionRecord());
		return "doctor/pharmacy";
	}

	@PostMapping("/medicine/getMedinice")
	public String postMedinice(@RequestBody String key) {
		doctorService.finishOnePatient(Integer.parseInt(key));
		return "doctor/pharmacy";
	}

	@GetMapping("/medicine/doubleCheck")
	public String prescriptionPage(Model model) {
		model.addAttribute("list", doctorService.getPreRecord());
		return "doctor/apothecarySystem";
	}

	@PostMapping("/medicine/doubleCheck")
	public String doubleCheck(@RequestBody String id, @CookieValue("id") String doctorId) {
		doctorService.finishPreRecord(Integer.parseInt(id), Integer.parseInt(doctorId));
		return "redirect:/doctor/medicine/doubleCheck";
	}

	@GetMapping("/treating")
	public String treatingPage(HttpSession session, Model model,
			@RequestParam(value = "addList", required = false) String addList,
			@RequestParam(value = "medicalId", required = false) String medicalId) {
		model.addAttribute("medicineSelect", doctorService.getMedicineList());
		if (medicalId != null) {
			model.addAttribute("medical", doctorService.getMedicalRecord(Integer.parseInt(medicalId)));
			return "doctor/doctorSystem::medicalRecord";
		}
		return "doctor/doctorSystem";
	}

	@PostMapping("/treating/prescribe")
	public String prescribe(@RequestBody PrescriptionRecord preRecord, @CookieValue("id") String id) {
		preRecord.setDoctorId(Integer.parseInt(id));
		doctorService.setPrescription(preRecord);
		return "redirect:/doctor/treating";
	}

	@GetMapping("/personal")
	public String doctorInfo(Model model, @CookieValue("id") String id,
			@RequestParam(value = "update", required = false) String update) {
		if (update != null) {
			model.addAttribute("accountInfo", "change");
		} else {
			model.addAttribute("accountInfo", "onshow");
		}
		model.addAttribute("doctor", doctorService.getDoctorInfo(Integer.parseInt(id)));
		return "doctor/staffInfo";
	}

	@PostMapping("/personal")
	public String doctorInfoChange(@CookieValue("id") String id, UserDoctor userDoctor) {
		userDoctor.setId(Integer.parseInt(id));
		doctorService.updateDoctorInfo(userDoctor);
		return "redirect:/doctor/personal";
	}

	@GetMapping("/login")
	public String login(HttpSession session, Model model) {
		if (session.getAttribute("message") != null) {
			model.addAttribute("error", session.getAttribute("message"));
		}
		return "loginDoctor";
	}

	@PostMapping("/login")
	public String loginbyDoctor(String account, String password, HttpSession session) {
		UserDoctor userDoctor = doctorService.loginbyDoctor(account, password);
		if (userDoctor != null) {
			setCookie(Integer.toString(userDoctor.getId()), userDoctor.getRole());
			session.setAttribute("userSection", userDoctor.getSection());
			session.setAttribute("userPosition", userDoctor.getPosition());
			session.setAttribute("userName", userDoctor.getName());
			return "redirect:/doctor";
		} else {
			session.setAttribute("message", "账号或密码错误，请重试");
			return "redirect:/doctor/login";
		}
	}

}
